/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatsoft.client.buz;

import ChatSoft.model.ChatMessage;
import ChatSoft.model.User;
import ChatSoft.model.UserData;
import ChatSoft.model.VoiceRequestMessage;
import ChatSoft.model.buz.SendObject;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

/**
 *
 * @author VinhThien
 */
public class VoiceController extends Thread {

    public BufferedOutputStream out;
    public BufferedInputStream input;
    public Socket svSocket;
    public Object recieveObject = null;
    private User user;
    SendObject sendObject;
    UserData dataTranfer;
    ChatMessage chatMessage;
    private String UserName;
    ChatBussiness chatBussiness;
    SourceDataLine sourceDataLine;
    TargetDataLine targetDataLine;
    byte tempBuffer[] = new byte[10000];

    public VoiceController(Socket sk) throws IOException {
        this.svSocket = sk;
        input = new BufferedInputStream(svSocket.getInputStream());
        out = new BufferedOutputStream(svSocket.getOutputStream());
    }

    @Override
    public void run() {
        try {
            while (input.read(tempBuffer) != -1) {
                            sourceDataLine.write(tempBuffer, 0, 10000);
                        }
        } catch (IOException ex) {
            Logger.getLogger(VoiceController.class.getName()).log(Level.SEVERE, null, ex);
        }
        Thread captureThread = new VoiceController.CaptureThread();
        captureThread.start();
    }
    
    public class CaptureThread extends Thread {
        public byte tempBuffer[] = new byte[10000];

        public void run() {
            try {
                while (true) {
                    int cnt = targetDataLine.read(tempBuffer, 0,
                            tempBuffer.length);
                    out.write(tempBuffer);
                }

            } catch (Exception e) {
                System.out.println(e);
                System.exit(0);
            }
        }
    }
}
